package com.lyr;

import com.lyr.dao.UserRepository;
import com.lyr.entity.User;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

/**
 * @author liyiruo
 * @Description
 * @Date 2024/8/15 下午4:09
 */
@SpringBootApplication
public class MongodbApp {
    public static void main(String[] args) {
        SpringApplication.run(MongodbApp.class, args);
    }

    @Bean
    public CommandLineRunner demo(UserRepository userRepository) {
        return (args) -> {
            // 创建一个用户对象
            User user = new User();
            user.setName("John Doe");
            user.setId("3");

            // 保存用户到数据库
            userRepository.save(user);

            // 读取用户
            User retrievedUser = userRepository.findById(user.getId()).orElse(null);
            System.out.println("Retrieved User: " + retrievedUser.getName());
        };
    }
}
